<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="cmdDataList">
      <!-- <el-table-column label="执行命令" align="center" prop="msgName" /> -->
      <el-table-column :label="$t('deviceDetail.controlLog.commandDevice')" align="center" prop="deviceName" />
      <el-table-column :label="$t('deviceDetail.controlLog.commandContent')" align="center" prop="content" width="300"/>
      <el-table-column :label="$t('deviceDetail.controlLog.valueDisplay')" align="center" prop="valueDisplay" width="300"/>
      <el-table-column :label="$t('deviceDetail.controlLog.executeTime')" align="center" prop="createTime" width="150" />
      <el-table-column :label="$t('deviceDetail.controlLog.sendMessage')" align="center" prop="message">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['device:device:cmdLogView']"
            @click="viewJSON(scope.row)"
          >{{$t('common.view')}}</el-button>
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceDetail.controlLog.sendStatus')" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cmd_execute_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceDetail.controlLog.responseTime')" align="center" prop="replyTime" width="150" />
      <el-table-column :label="$t('deviceDetail.controlLog.responseMessage')" align="center" prop="replyMessaeg">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['device:device:cmdLogView']"
            @click="viewReplyJSON(scope.row)"
          >{{$t('common.view')}}</el-button>
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceDetail.controlLog.responseStatus')" align="center" prop="cmdStatus" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

     <!-- json展示对话框 -->
    <el-dialog :title="jsonTitle" :visible.sync="jsonPopup" width="800px" append-to-body>
      <v-code
        :value="json"
        language="javascript"
        style="height: 500px"
      ></v-code>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="jsonPopup=false">{{ $t('common.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCmdData, getCmdData, delCmdData, addCmdData, updateCmdData } from "@/api/data/cmdData";
import code from '../cmdData/code'
export default {
  props:{
    deviceId:'',
    sensorId:''
  },
  dicts: ['cmd_execute_status'],
  components:{'v-code':code},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备命令执行日志表格数据
      cmdDataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        msgId: null,
        msgName: null,
        status: null,
      },
      // 表单参数
      form: {},
      jsonPopup:false,
      jsonTitle:'',
      json:''
    };
  },
  created() {
    if(this.deviceId){
      this.queryParams.deviceId = this.deviceId;
      this.queryParams.sensorId = this.sensorId||'';
      this.getList();
    }
  },
  methods: {
    /** 查询设备命令执行日志列表 */
    getList() {
      this.loading = true;
      listCmdData(this.queryParams).then(response => {
        this.cmdDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyId: null,
        msgId: null,
        msgName: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCmdData(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.successful'));
              this.open = false;
              this.getList();
            });
          } else {
            addCmdData(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.successful'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('common.deleteData',{name:row.name})).then(function() {
        return delCmdData(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('common.successful'));
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('data/cmdData/export', {
        ...this.queryParams
      }, `cmdData_${new Date().getTime()}.xlsx`)
    },
    /** json展示框 **/
    viewJSON(res){
      this.jsonTitle = res.topic||''
      if(res.message.length>0){
        if(res.message[0]==='{'&&res.message[res.message.length-1]==='}'){
          this.json = JSON.stringify(JSON.parse(res.message),null,'\t');
        }else{
          this.json = res.message
        }
      }
      this.jsonPopup = true;
    },
    viewReplyJSON(res){
      this.jsonTitle = res.replyTopic||''
      if(res.replyMessaeg.length>0){
        if(res.replyMessaeg[0]==='{'&&res.replyMessaeg[res.replyMessaeg.length-1]==='}'){
          this.json = JSON.stringify(JSON.parse(res.replyMessaeg),null,'\t');
        }else{
          this.json = res.replyMessaeg
        }
      }
      this.jsonPopup = true;
    }
  },
  watch:{
    sensorId(v){
      if(v){
        this.queryParams.deviceId = this.deviceId;
        this.queryParams.sensorId = this.sensorId||'';
        this.getList();
      }
    }
  }
};
</script>
